package org.wlldTest.lesson12;

import org.wlld.config.RZ;
import org.wlld.entity.ThreeChannelMatrix;
import org.wlld.function.ReLu;
import org.wlld.function.Tanh;
import org.wlld.matrixTools.Matrix;
import org.wlld.nerveCenter.ModelParameter;
import org.wlld.nerveCenter.NerveManager;
import org.wlld.nerveEntity.SensoryNerve;
import org.wlldTest.lesson8.Back;

import java.io.DataOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Test {
    public static NerveManager nerveManager;

    static {
        try {
            nerveManager = new NerveManager(3, 9, 2,
                    1, new Tanh(), 0.01, RZ.NOT_RZ, 0, 0);
            nerveManager.initImageNet(2, 3, 23, 23, false, true, 0.01,
                    new ReLu());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void putPicture(ThreeChannelMatrix pic, boolean isStudy, Map<Integer, Double> E, Back back) throws Exception {
        List<SensoryNerve> sensoryNerves = nerveManager.getSensoryNerves();
        for (int i = 0; i < 3; i++) {
            Matrix matrix = null;
            SensoryNerve sensoryNerve = sensoryNerves.get(i);
            switch (i) {
                case 0:
                    matrix = pic.getMatrixR();
                    break;
                case 1:
                    matrix = pic.getMatrixG();
                    break;
                case 2:
                    matrix = pic.getMatrixB();
                    break;
            }
            sensoryNerve.postMatrixMessage(1, matrix, isStudy, E, back, false);
        }
    }

    public static void main(String[] args) throws Exception {
        study();
        //test();
    }

    private static ThreeChannelMatrix getPicture() {
        ThreeChannelMatrix th1 = new ThreeChannelMatrix();
        Matrix r1 = new Matrix(23, 23);
        Matrix g1 = new Matrix(23, 23);
        Matrix b1 = new Matrix(23, 23);
        th1.setMatrixR(r1);
        th1.setMatrixG(g1);
        th1.setMatrixB(b1);
        th1.setX(23);
        th1.setY(23);
        return th1;
    }

    public static void test() throws Exception {
        Random random = new Random();
        for (int t = 0; t < 10; t++) {
            Back back = new Back();
            System.out.println("================================" + t);
            ThreeChannelMatrix t1 = getPicture();
            ThreeChannelMatrix t2 = getPicture();
            double r1 = random.nextDouble() * 0.1;
            double g1 = r1 + 0.2;
            double b1 = r1 + 0.3;
            double r2 = random.nextDouble() * 0.1 + 0.2;
            double g2 = r2 + 0.5;
            double b2 = r2 + 0.2;
            for (int i = 0; i < 23; i++) {
                for (int j = 0; j < 23; j++) {
                    t1.getMatrixR().setNub(i, j, r1);
                    t1.getMatrixG().setNub(i, j, g1);
                    t1.getMatrixB().setNub(i, j, b1);
                    t2.getMatrixR().setNub(i, j, r2);
                    t2.getMatrixG().setNub(i, j, g2);
                    t2.getMatrixB().setNub(i, j, b2);
                }
            }
            //putPicture(t1, false, null, back);
            putPicture(t2, false, null, back);
            System.out.println("v1:" + back.getV1());
            System.out.println("v2:" + back.getV2());

        }
    }

    public static void study() throws Exception {
        Random random = new Random();
        Map<Integer, Double> E1 = new HashMap<>();
        Map<Integer, Double> E2 = new HashMap<>();
        for (int t = 0; t < 2000; t++) {
            System.out.println("================================" + t);
            E1.clear();
            E2.clear();
            E1.put(1, -0.5);
            E1.put(2, -0.5);
            E2.put(1, 0.5);
            E2.put(2, 0.5);
            ThreeChannelMatrix t1 = getPicture();
            ThreeChannelMatrix t2 = getPicture();
            double r1 = random.nextDouble() * 0.1;
            double g1 = r1 + 0.2;
            double b1 = r1 + 0.3;
            double r2 = random.nextDouble() * 0.1 + 0.2;
            double g2 = r2 + 0.5;
            double b2 = r2 + 0.2;
            for (int i = 0; i < 23; i++) {
                for (int j = 0; j < 23; j++) {
                    t1.getMatrixR().setNub(i, j, r1);
                    t1.getMatrixG().setNub(i, j, g1);
                    t1.getMatrixB().setNub(i, j, b1);
                    t2.getMatrixR().setNub(i, j, r2);
                    t2.getMatrixG().setNub(i, j, g2);
                    t2.getMatrixB().setNub(i, j, b2);
                }
            }
            putPicture(t1, true, E1, null);
            putPicture(t2, true, E2, null);
        }
    }
}
